const db = require("../db/index");
const fs = require('fs')

//上传轮播图
exports.uploadSwiper = (req, res) => {
  let oldName = req.files[0].filename;
  let newName = Buffer.from(req.files[0].originalname, "latin1").toString(
    "utf8"
  );
  fs.renameSync('./public/upload/' + oldName,'./public/upload/' + newName)
  const sql = "update setting set set_value=? where set_name=?";
  db.query(
    sql,
    [`http://127.0.0.1:3000/upload/${newName}`, req.body.name],
    (err, results) => {
      if (err) return res.cc(err);
      res.send({
        status: 0,
        message: "上传成功",
      });
    }
  );
};
// 获取全部轮播图
exports.getAllSwiper = (req, res) => {
  const sql = "select * from setting where set_name like 'swiper%'";
  db.query(sql, (err, results) => {
    if (err) return res.cc(err);
    res.send({
      status: 0,
      message: "获取轮播图成功",
      results,
    });
  });
};
// 获取公司名称
exports.getCompanyName = (req, res) => {
  const sql = "select * from setting where set_name like '公司名称'";
  db.query(sql, (err, results) => {
    if (err) return res.cc(err);
    res.send(results);
  });
};
// 修改公司名字
exports.changeCompanyName = (req, res) => {
  const sql = "update setting set set_value=? where set_name='公司名称'";
  db.query(sql, req.body.set_value, (err, results) => {
    if (err) return res.cc(err);
    res.send({
      status: 0,
      message: "修改公司成功",
    });
  });
};
// 编辑公司介绍
exports.changeCompanyIntroduce = (req, res) => {
  const sql = "update setting set set_text=? where set_name=?";
  db.query(sql, [req.body.set_value, req.body.set_name], (err, results) => {
    if (err) return res.cc(err);
    res.send({
      status: 0,
      message: "修改公司介绍成功",
    });
  });
};
// 获取公司介绍
exports.getCompanyIntroduce = (req, res) => {
  const sql = "select * from setting where set_name=?";
  db.query(sql, req.body.set_name, (err, results) => {
    if (err) return res.cc(err);
    res.send(results[0].set_text);
  });
};
// 获取所有公司信息
exports.getAllCompanyIntroduce = (req, res) => {
  const sql = "select * from setting where set_name like '公司%'";
  db.query(sql, (err, results) => {
    if (err) return res.cc(err);
    res.send(results);
  });
};
// 部门设置 传入数组
exports.setDepartment = (req,res) => {
  const sql = 'update setting set set_value=? where set_name="部门设置"'
  db.query(sql,req.body.set_value,(err, results) =>{
    if(err) return res.cc(err)
    res.send({
      status:0,
      message: '部门设置成功'
    })
  })
}
// 产品设置 传入数组
exports.setProduct = (req,res) => {
  const sql = 'update setting set set_value=? where set_name="产品设置"'
  db.query(sql,req.body.set_value,(err, results) =>{
    if(err) return res.cc(err)
    res.send({
      status:0,
      message: '产品设置成功'
    })
  })
}
// 获取部门
exports.getDepartment = (req,res) => {
  const sql = 'select set_value from setting where set_name="部门设置"'
  db.query(sql, (err, results) =>{
    if(err) return res.cc(err)
    res.send(results[0].set_value)
  })
}
// 获取产品
exports.getProduct = (req,res) => {
  const sql = 'select set_value from setting where set_name="产品设置"'
  db.query(sql, (err, results) =>{
    if(err) return res.cc(err)
    res.send(results[0].set_value)
  })
}